package com.kexio.dynamic.feature;

import com.kexio.dynamic.feature.aspect.FeatureToggleAspect;
import com.kexio.dynamic.feature.service.FeatureToggleService;
import com.kexio.dynamic.feature.service.impl.DefaultFeatureToggleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import jakarta.annotation.PostConstruct;

/**
 * 功能开关模块自动配置
 * 
 * 功能说明：
 * - 自动配置功能开关服务
 * - 自动配置功能开关AOP切面
 * - 启用AspectJ自动代理
 * 
 * @author Kexio
 * @since 1.0.0
 */
@Configuration
@EnableAspectJAutoProxy
@ConditionalOnProperty(name = "kexio.dynamic.feature.enabled", havingValue = "true", matchIfMissing = true)
public class FeatureToggleAutoConfiguration {

    private static final Logger log = LoggerFactory.getLogger(FeatureToggleAutoConfiguration.class);

    @PostConstruct
    public void init() {
        log.info("==> Kexio功能开关模块已启用");
        log.debug("功能开关模块包含: 功能开关服务、AOP切面、策略支持");
    }

    /**
     * 功能开关服务
     */
    @Bean
    @ConditionalOnMissingBean
    public FeatureToggleService featureToggleService() {
        log.debug("==> 创建FeatureToggleService实例");
        return new DefaultFeatureToggleService();
    }

    /**
     * 功能开关AOP切面
     */
    @Bean
    @ConditionalOnMissingBean
    public FeatureToggleAspect featureToggleAspect(FeatureToggleService featureToggleService) {
        log.debug("==> 创建FeatureToggleAspect实例");
        return new FeatureToggleAspect(featureToggleService);
    }
}
